{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1_CAy-bNBSxtRrdlHx8cNs5FMj4ko9Y9b?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### [Cohere](https://cohere.com/) - Cohere's LLM API call.\n"
      ],
      "metadata": {
        "id": "HxxOWz9dpsYj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Install required libraries"
      ],
      "metadata": {
        "id": "Y3axTI0sp5Hg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain-cohere==0.4.2"
      ],
      "metadata": {
        "id": "ShxTNxM5gqtr",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9b8cde1d-cb5a-4661-d2d9-d879736a5552"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/42.2 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m42.2/42.2 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m252.9/252.9 kB\u001b[0m \u001b[31m8.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m33.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.3/3.3 MB\u001b[0m \u001b[31m44.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.9/50.9 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import related libraries"
      ],
      "metadata": {
        "id": "9jJ1vqs-p_Zx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_cohere import ChatCohere"
      ],
      "metadata": {
        "id": "RL-3LsYogoH5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import getpass\n",
        "import os"
      ],
      "metadata": {
        "id": "GT55z5AkhyOW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Provide a Cohere API key (Traial & Paid one)\n",
        "\n",
        "[Cohere API Key Creation Link](https://dashboard.cohere.com/api-keys)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "F6UeDlrgqI2A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"COHERE_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yobvrD3glfd4",
        "outputId": "3c1fac2a-ce55-4322-9916-6ed36b76d6fe"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatCohere(\n",
        "    model=\"command-r-plus-08-2024\",\n",
        "    temperature=0.5\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    (\n",
        "        \"system\",\n",
        "        \"You are a helpful assistant that explains problems step-by-step.\",\n",
        "    ),\n",
        "    (\"human\", \"Solve this problem step by step: {problem}\"),\n",
        "]"
      ],
      "metadata": {
        "id": "HHXlnTcxpi-8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "ai_msg = llm.invoke(\"Provide me python code of sudoku\")\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f4OZQ-kvqNl5",
        "outputId": "bc5544f1-0ea2-4ea3-ffc9-ef3e197eb8d5"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Here is a simple text-based Sudoku game implemented in Python. This code will allow you to play Sudoku in the terminal:\n",
            "\n",
            "```python\n",
            "def display_board(board):\n",
            "    \"\"\"\n",
            "    Display the Sudoku board in a readable format.\n",
            "    \"\"\"\n",
            "    for i in range(0, 9, 3):\n",
            "        for j in range(0, 9, 3):\n",
            "            row = ''\n",
            "            for k in range(3):\n",
            "                row += '| ' + ' '.join(str(num) if num != 0 else ' ' for num in board[i + k][j:j + 3]) + ' '\n",
            "            print(row + '|')\n",
            "        print('------+-------+------')\n",
            "\n",
            "def is_valid(board, row, col, num):\n",
            "    \"\"\"\n",
            "    Check if a number can be placed at the given position.\n",
            "    \"\"\"\n",
            "    # Check row\n",
            "    for i in range(9):\n",
            "        if board[row][i] == num:\n",
            "            return False\n",
            "\n",
            "    # Check column\n",
            "    for i in range(9):\n",
            "        if board[i][col] == num:\n",
            "            return False\n",
            "\n",
            "    # Check 3x3 grid\n",
            "    grid_row, grid_col = row // 3 * 3, col // 3 * 3\n",
            "    for i in range(grid_row, grid_row + 3):\n",
            "        for j in range(grid_col, grid_col + 3):\n",
            "            if board[i][j] == num:\n",
            "                return False\n",
            "\n",
            "    return True\n",
            "\n",
            "def solve_sudoku(board):\n",
            "    \"\"\"\n",
            "    Solve the Sudoku puzzle using backtracking.\n",
            "    \"\"\"\n",
            "    for i in range(9):\n",
            "        for j in range(9):\n",
            "            if board[i][j] == 0:\n",
            "                for num in range(1, 10):\n",
            "                    if is_valid(board, i, j, num):\n",
            "                        board[i][j] = num\n",
            "                        if solve_sudoku(board):\n",
            "                            return True\n",
            "                        board[i][j] = 0\n",
            "                return False\n",
            "    return True\n",
            "\n",
            "def play_sudoku():\n",
            "    board = [[0] * 9 for _ in range(9)]\n",
            "    # Place initial numbers (you can modify these)\n",
            "    board[0] = [5, 3, 0, 0, 7, 0, 0, 0, 0]\n",
            "    board[1] = [6, 0, 0, 1, 9, 5, 0, 0, 0]\n",
            "    board[2] = [0, 9, 8, 0, 0, 0, 0, 6, 0]\n",
            "    board[3] = [8, 0, 0, 0, 6, 0, 0, 0, 3]\n",
            "    board[4] = [4, 0, 0, 8, 0, 3, 0, 0, 1]\n",
            "    board[5] = [7, 0, 0, 0, 2, 0, 0, 0, 6]\n",
            "    board[6] = [0, 6, 0, 0, 0, 0, 2, 8, 0]\n",
            "    board[7] = [0, 0, 0, 4, 1, 9, 0, 0, 5]\n",
            "    board[8] = [0, 0, 0, 0, 8, 0, 0, 7, 9]\n",
            "\n",
            "    display_board(board)\n",
            "    print(\"Solving...\")\n",
            "    solve_sudoku(board)\n",
            "    display_board(board)\n",
            "\n",
            "if __name__ == \"__main__\":\n",
            "    play_sudoku()\n",
            "```\n",
            "\n",
            "This code defines functions to display the Sudoku board, check the validity of a number in a position, and solve the puzzle using backtracking. The `play_sudoku` function sets up the initial board and then calls the solver.\n",
            "\n",
            "You can run this code to play a game of Sudoku. Feel free to modify the initial board values to try different puzzles.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "6S4n9GOir1Us"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}